#!/usr/bin/python
import random, math, re

file = "quotes.txt"

def printrandomquote():
	f = open(file, 'r')
	quotes = f.readlines()
	length = len(quotes)-1
	num = random.randint(0,length)
	quote = quotes[num]
	num = str(int(num)+1)
	quote = "Quote #%s: %s"%(num,quote)
	print "%s \n" % (quote)
	return quote
	f.close()

def printquote(num):
	f = open(file, 'r')
	quotes = f.readlines()
	length = len(quotes)
	if num > length:
		quote = "Quote %s not found - %s quotes in the system" % (num,length)
	else:
		quote = quotes[num-1]
		quote = "Quote #%s: %s"%(num,quote)
	return quote
	f.close()

def addquote(quote):
	f = open(file, 'a')
	f.write("%s\n" % quote)
	f.close()
	f = open(file,'r')
	quotes = f.readlines()
	length = len(quotes)
	return length
	f.close()

def searchquote(regex):
	f = open(file,'r')
	quotes = f.readlines()
	num=1
	full = list()
	regex = "(?i)%s" % regex
	for searching in quotes:
		if re.search(regex,searching) != None:
			searching = "Quote #%s: %s" % (num,searching)
			full.append(searching)
		print num
		num = num+1

	length = len(full)-1
	rannum = random.randint(0,length)
	quote = full[rannum]

	return quote
